﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Dynamic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HouseManger.Model;

namespace HouseManager.DAL
{
    public class MangerDAL
    {
        HouseEntity HouseEntity = new HouseEntity();
        /// <summary>
        /// 列表显示
        /// </summary>
        /// <returns></returns>
        public List<HouseList> GetList()
        {
            var list = HouseEntity.HouseInfo.Select(h => new HouseList
            {
                Id = h.Id,
                Name = h.Name,
                Area = h.Area,
                Floot = h.Floot,
                State = h.State,
                Describe = h.Describe,
                FloorCount = h.FloorCount,
                Price = h.Price,
                TypeName = h.HouseType.TName,
                Years = h.Years
            }).ToList();
            return list;
        }
        /// <summary>
        /// 查询小区名称
        /// </summary>
        /// <param name="Name"></param>
        /// <returns></returns>
        public List<HouseList> GetListName(string Name)
        {
            var list = HouseEntity.HouseInfo.Select(h => new HouseList
            {
                Id = h.Id,
                Name = h.Name,
                Area = h.Area,
                Floot = h.Floot,
                State = h.State,
                Describe = h.Describe,
                FloorCount = h.FloorCount,
                Price = h.Price,
                TypeName = h.HouseType.TName,
                Years = h.Years
            }).Where(h => h.Name.Contains(Name)).ToList();
            return list;
        }

        /// <summary>
        /// 获取房屋类型
        /// </summary>
        /// <returns></returns>
        public List<HouseType> GetHouseType()
        {
            var type = HouseEntity.HouseType.ToList();
            return type;    
        }
        /// <summary>
        /// 添加信息
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public int Add(HouseInfo info)
        {
            HouseEntity.HouseInfo.Add(info);    
            return HouseEntity.SaveChanges();
        }
        /// <summary>
        /// 修改上下架
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool UpdateState(int id)
        {
            HouseInfo houseInfo = HouseEntity.HouseInfo.Find(id);
            houseInfo.State = houseInfo.State == 0 ? 1 : 0;
            HouseEntity.Entry(houseInfo).State = EntityState.Modified;
            return HouseEntity.SaveChanges()>0;
        }

        /// <summary>
        /// 查询要编辑的数据Id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public HouseInfo SelectId(int id)
        {
            HouseInfo houseInfo = HouseEntity.HouseInfo.FirstOrDefault(a => a.Id == id);
            return houseInfo;
        }
        /// <summary>
        /// 修改信息
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public int EditInfo(HouseInfo info)
        {
            HouseInfo house = HouseEntity.HouseInfo.SingleOrDefault(t => t.Id == info.Id);
            if (house == null)
            {
                return 0;
            }
            house.Name = info.Name;
            house.Area = info.Area;
            house.Price = info.Price;
            house.FloorCount = info.FloorCount;
            house.Floot = info.Floot;
            house.State = info.State;
            house.Describe = info.Describe;
            house.Years = info.Years;
            house.TypeId = info.TypeId;
            HouseEntity.Entry(house).State = EntityState.Modified;
            return HouseEntity.SaveChanges();
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int DeleteInfo(int id)
        {
            HouseInfo res = HouseEntity.HouseInfo.Find(id);
            HouseEntity.HouseInfo.Remove(res);
            int result=HouseEntity.SaveChanges();   
            return result;  
        }
    }
}
